<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="查询方式" prop="noticeType">
        <el-select v-model="searchType" placeholder="请选择" clearable style="width:100px">
          <el-option label="学校" value="name"></el-option>
          <el-option label="账号" value="phone"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item  v-show="searchType == 'phone'">
        <el-input v-model="queryParams.mobile" placeholder="请输入账号" clearable />
      </el-form-item>
      <el-form-item  v-show="searchType == 'name'">
        <el-input v-model="queryParams.merchantName" placeholder="请输入学校" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" @click="handleAdd">删除</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableData" border>
      <!-- <el-table-column type="selection" width="50" align="center" /> -->
      <el-table-column label="学校" align="center" prop="merchantName" />
      <el-table-column label="账号" align="center" prop="mobile" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)" plain icon="el-icon-edit">修改</el-button>
          <!-- <el-button type="success" size="mini"  @click="changeStatus(scope.row)" plain icon="el-icon-edit">启用</el-button> -->
          <!-- <el-button type="danger" size="mini"  @click="changeStatus(scope.row)" plain icon="el-icon-edit">禁用</el-button> -->
          <!-- <el-button type="danger" size="mini"  @click="handleDelete(scope.row)" plain icon="el-icon-edit">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 新增或修改账户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body style="margin-top:50px;">
      <el-form ref="form" :model="form" label-width="150px">
        <el-form-item label="学校名称" prop="merchantName">
          <el-input v-model="form.merchantName" placeholder="请输入学校名称" clearable style="width:240px;" />
        </el-form-item>
        <el-form-item label="账号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入账号" clearable style="width:240px;" />
        </el-form-item>
        <el-form-item label="登录密码" prop="password" v-show="title!='账户修改'">
          <el-input v-model="form.password" placeholder="请输入登录密码" clearable style="width:240px;" />
        </el-form-item>
        <el-form-item label="姓名" prop="name" v-show="title!='账户修改'">
          <el-input v-model="form.name" placeholder="请输入姓名" clearable style="width:240px;" />
        </el-form-item>
        <el-form-item label="姓名" v-show="title=='账户修改'">
          <el-input v-model="form.adminName" placeholder="请输入姓名" clearable style="width:240px;" />
        </el-form-item>
        <el-form-item label="新密码" v-show="title=='账户修改'">
          <el-input v-model="form.newPassword" placeholder="请输入新密码" clearable style="width:240px;" type="password" show-password />
        </el-form-item>
        <el-form-item label="确认密码" v-show="title=='账户修改'">
          <el-input v-model="form.verifyPassword" placeholder="请确认新密码" clearable style="width:240px;" type="password" show-password/>
        </el-form-item>
        <el-form-item label="状态" v-show="title=='账户修改'">
          <el-radio-group v-model="form.status">
                <el-radio label="正常"></el-radio>
                <el-radio label="停用"></el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { getForm ,addMerchant ,deleteAdmin ,echoAdmin ,changeAdmin} from "@/api/equipmentCenter/index.js";

export default {
  data() {
    return {
      searchType: "name",
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      tableData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
         current: 0,
         merchantName: "",
         mobile: "",
         pageSize: 10
      },
      // 表单参数
      form: {
         merchantName: "",
         mobile: "",
         name: "",
         password: ""
      },
      // 表单校验
      rules: {
        merchantName: [
          { required: true, message: "学校名称不能为空", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "账号不能为空", trigger: "blur" }
        ],
        // name: [
        //   { required: true, message: "姓名不能为空", trigger: "blur" }
        // ],
        // password: [
        //   { required: true, message: "密码不能为空", trigger: "blur" }
        // ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    changeStatus(e){

    },
    /** 查询列表 */
        getList() {
          // this.loading = true;
          getForm(this.queryParams).then(res => {
            console.log(res);
            // this.loading = false;
            if (res.data.success == true) {
              this.tableData = res.data.data.list;
              this.total = res.data.data.total;
              // this.loading = false;
            } else {
              this.$message.info(res.data.message)
            }
          }).catch(error => {
            console.log(error.response.status);
            if (error.response.status == 401) {
              this.$confirm('登录状态过期, 请重新登录!', '提示', {
                confirmButtonText: '确定',
                // cancelButtonText: '取消',
                showCancelButton: false,
                type: 'warning'
              }).then(() => {
                this.$router.push('/login')
              }).catch(() => {

              })
            }
          })
        },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        noticeId: undefined,
        noticeTitle: undefined,
        noticeType: undefined,
        noticeContent: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 0;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      // this.handleQuery();
      this.queryParams={
        current: 0,
        merchantName: "",
        mobile: "",
        pageSize: 10
      }
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.noticeId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      this.open = true;
      this.title = "开通账户";
      this.form={
        merchantName: "",
        mobile: "",
        name: "",
        password: ""
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form={
         adminId: "",
         adminName: "",
         merchantId: "",
         merchantName: "",
         mobile: "",
         newPassword: "",
         status: "",
         verifyPassword: ""
      }
      // this.reset();
      this.open = true;
      this.title = "账户修改";
      echoAdmin({ merchantId : row.merchantId }).then(res => {
        console.log(res)
        if (res.data.success == true) {
           if(res.data.data){
             this.form={
                adminId: res.data.data.adminId,
                adminName: res.data.data.name,
                merchantId: res.data.data.merchantId,
                merchantName: res.data.data.merchantName,
                mobile: res.data.data.mobile,
                newPassword: "",
                status: res.data.data.status,
                verifyPassword: ""
             }
           }
        }
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.title == "开通账户"){
            addMerchant(this.form).then(res=>{
               if (res.data.success == true) {
                    this.open = false
                    this.$message({
                      message: '添加成功!',
                      duration: 1000,
                      type: 'success',
                      onClose: () => {
                        this.resetQuery()
                      }
                    })
                }
            }).catch(err=>{
              console.log(err.response);
              this.$message.info(err.response.data.message)
            })
          }else{
            var data=JSON.parse(JSON.stringify(this.form))
            if(data.status=='正常'){
              data.status=0
            }else{
              data.status=1
            }
            if(data.newPassword!=data.verifyPassword){
              this.$message.info('新密码与确认密码不一致')
              return false
            }
             changeAdmin(data).then(res=>{
                if (res.data.success == true) {
                     this.open = false
                     this.$message({
                       message: '修改成功!',
                       duration: 1000,
                       type: 'success',
                       onClose: () => {
                         this.resetQuery()
                       }
                     })
                 }
             })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('此操作将删除该账户, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              deleteAdmin({ merchantId : row.merchantId }).then(res => {
                console.log(res)
                if (res.data.success == true) {
                  this.$message({
                    message: '删除成功!',
                    duration: 1000,
                    type: 'success',
                    onClose: () => {
                      // this.$router.go(0)
                      this.resetQuery()
                    }
                  })
                }
              })
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消删除'
              })
            })
    }
  }
};
</script>
